/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.yxw.yxnet_cd_center.customer_service.utils;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 *
 * @author Administrator
 */
public class Aes128 {
    //获取Cipher对象的算法
    private static String transformation = "AES/CBC/PKCS5Padding";

    //加解密密钥
    private static String LOGIN_SECRET_KEY = "custom-service-system@2023";

    //算法
    private static String ALGORITHM = "AES";

    //字符编码
    private static final String encoding = "UTF-8";

  /**
   * 加密
   * @param input  明文
   * @return  返回密文
   * @throws Exception
   */
    public static BaseResult Aes128EncryptUtf8(String pwd,String encryptString) 
    {
        Cipher cipher = null;
        byte[] bytesEncryptString = null;
        try
        {
            bytesEncryptString = encryptString.getBytes(encoding);
        }
        catch(UnsupportedEncodingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }    
        try
        {
            // 1,获取Cipher对象  throws Exception
            cipher = Cipher.getInstance(transformation);
        }
        catch(NoSuchAlgorithmException | NoSuchPaddingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }
        SecretKeySpec sks = null;
        IvParameterSpec iv = null;
        try
        {
            // 指定密钥规则
            sks = new SecretKeySpec(pwd.getBytes(encoding), ALGORITHM);
            // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度  aes 16个字节长度
            //这里为了方便,统一使用传来的秘钥
            iv = new IvParameterSpec(pwd.getBytes(encoding));        
        }
        catch(UnsupportedEncodingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }

        try
        {
            cipher.init(Cipher.ENCRYPT_MODE,sks,iv);
        }
        catch(InvalidAlgorithmParameterException | InvalidKeyException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }    
        // 3. 加密
        byte[] bytes = null;
        try
        {
            bytes = cipher.doFinal(bytesEncryptString);
        }
        catch(BadPaddingException | IllegalBlockSizeException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }    
        String encode = Base64.encodeBase64String(bytes);
        return BaseResult.success(encode);
    }

    /**
    * 解密
    * @param input  密文
    * @return 返回原文
    * @throws Exception
    */
    public static BaseResult Aes128DecryptUtf8(String pwd,String decryptString)
    {
        Cipher cipher = null;
        try
        {
            // 1,获取Cipher对象  throws Exception
            cipher = Cipher.getInstance(transformation);
        }
        catch(NoSuchAlgorithmException | NoSuchPaddingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }        
        SecretKeySpec sks = null;
        IvParameterSpec iv = null;
        try
        {
            // 指定密钥规则
            sks = new SecretKeySpec(pwd.getBytes(encoding), ALGORITHM);
            // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度  aes 16个字节长度
            //这里为了方便,统一使用传来的秘钥
            iv = new IvParameterSpec(pwd.getBytes(encoding));        
        }
        catch(UnsupportedEncodingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }
        try
        {
            cipher.init(Cipher.DECRYPT_MODE,sks,iv);
        }
        catch(InvalidAlgorithmParameterException | InvalidKeyException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }      
        // 3. 解密
        byte[] bytes = null;
        try
        {
            bytes = cipher.doFinal(Base64.decodeBase64(decryptString));
        }
        catch(BadPaddingException | IllegalBlockSizeException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }
        try
        {
            return BaseResult.success(new String(bytes,"UTF-8"));
        }
        catch(UnsupportedEncodingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }        
    }
    /**
    * 加密
    * @param input  明文
    * @return  返回密文
    * @throws Exception
    */
    public static BaseResult Aes128Encrypt(String pwd,String encryptString)  
    {
        Cipher cipher = null;  
        try
        {
            // 1,获取Cipher对象  throws Exception
            cipher = Cipher.getInstance(transformation);
        }
        catch(NoSuchAlgorithmException | NoSuchPaddingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }
        SecretKeySpec sks = new SecretKeySpec(pwd.getBytes(), ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(pwd.getBytes());
        try
        {
            cipher.init(Cipher.ENCRYPT_MODE,sks,iv);
        }
        catch(InvalidAlgorithmParameterException | InvalidKeyException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }    
        // 3. 加密
        byte[] bytes = null;
        try
        {
            bytes = cipher.doFinal(encryptString.getBytes());
        }
        catch(BadPaddingException | IllegalBlockSizeException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }    
        String encode = Base64.encodeBase64String(bytes);
        return BaseResult.success(encode);
    }

    /**
    * 解密
    * @param input  密文
    * @return 返回原文
    * @throws Exception
    */
    public static BaseResult Aes128Decrypt(String pwd,String decryptString)
    {
        //throws Exception 
        //Cipher cipher = Cipher.getInstance(transformation);
        Cipher cipher = null;
        try
        {
            // 1,获取Cipher对象  throws Exception
            cipher = Cipher.getInstance(transformation);
        }
        catch(NoSuchAlgorithmException | NoSuchPaddingException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }        
        // 指定密钥规则
        SecretKeySpec sks = new SecretKeySpec(pwd.getBytes(), ALGORITHM);
        // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度  aes 16个字节长度
        //这里为了方便,统一使用传来的秘钥
        IvParameterSpec iv = new IvParameterSpec(pwd.getBytes());
        try
        {
            cipher.init(Cipher.DECRYPT_MODE,sks,iv);
        }
        catch(InvalidAlgorithmParameterException | InvalidKeyException oException)
        {
            return BaseResult.failed(oException.getMessage());
        }      
        //IvParameterSpec iv = new IvParameterSpec(LOGIN_SECRET_KEY.getBytes());
        //cipher.init(Cipher.DECRYPT_MODE, sks, iv);
        //byte[] bytes = cipher.doFinal(Base64.decodeBase64(input));
        //return new String(bytes);
        // 3. 解密
        byte[] bytes = null;
        try
        {
            bytes = cipher.doFinal(Base64.decodeBase64(decryptString));
        }
        catch(BadPaddingException | IllegalBlockSizeException oException)
        {
            return BaseResult.failed(oException.getMessage());
        } 
        return BaseResult.success(new String(bytes));
    }    
    /**
    * 加密
    * @param input  明文
    * @param key   密钥(AES,密钥的长度必须是16个字节)
    * @param algorithm   获取密钥的算法
    * @return  返回密文
    * @throws Exception
    */
    public static String encrypt(String input, String key)
    {
        String algorithm = "AES";
        try
        {
            // 1,获取Cipher对象
            Cipher cipher = Cipher.getInstance(transformation);
            // 指定密钥规则
            SecretKeySpec sks = new SecretKeySpec(key.getBytes(), algorithm);
            // 2.初始化向量的秘钥长度需要根据算法而定,des 8个字节长度  aes 16个字节长度
            //这里为了方便,统一使用传来的秘钥
            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, sks, iv);
        //cipher.init(Cipher.ENCRYPT_MODE, sks);
            // 3. 加密
            byte[] bytes = cipher.doFinal(input.getBytes());
            // 对数据进行Base64编码
        //String encode = Base64.encode(bytes);
            String encode = Base64.encodeBase64String(bytes);
            //String encode2 = new String(encode.getBytes(),encoding);
            return encode;
        }
        catch(Exception oException)
        {
                return "";
        } 
    }

    /**
    * 解密
    * @param input  密文
    * @param key   密钥(AES,密钥的长度必须是16个字节)
    * @param algorithm   获取密钥的算法
    * @return 返回原文
    * @throws Exception
    */
    public static String decrypt(String input, String key)
    {
        try
        {  
            //String algorithm = "AES";   
            Cipher cipher = Cipher.getInstance(transformation);
            SecretKeySpec sks = new SecretKeySpec(key.getBytes(), ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, sks, iv);
        //         cipher.init(Cipher.DECRYPT_MODE, sks);
        //    byte[] bytes = cipher.doFinal(Base64.decode(input));
            byte[] bytes = cipher.doFinal(Base64.decodeBase64(input));
            return new String(bytes);
        }
        catch(Exception oException)
        {
                return "";
        }
    }
    public static String getEncoding(String str) 
    {      
        String encode = "GB2312";      
        try {      
            if (str.equals(new String(str.getBytes(encode), encode))) {      //判断是不是GB2312
               String s = encode;      
              return s;      //是的话，返回“GB2312“，以下代码同理
            }      
        } 
        catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
        try {      
          if (str.equals(new String(str.getBytes(encode), encode))) {      //判断是不是ISO-8859-1
               String s1 = encode;      
              return s1;      
           }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
        try {      
          if (str.equals(new String(str.getBytes(encode), encode))) {   //判断是不是UTF-8
               String s2 = encode;      
              return s2;      
           }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
        try {      
          if (str.equals(new String(str.getBytes(encode), encode))) {      //判断是不是GBK
               String s3 = encode;      
              return s3;      
           }      
        } catch (Exception exception3) {      
        }      
        return "";        //如果都不是，说明输入的内容不属于常见的编码格式。
    }
}